#!/usr/bin python3
# -*- coding:UTF-8 -*-
# Author: nigo
import sys, fitz, os, datetime

def pyMuPDF_fitz(pdf_path, image_path):
    pdfDoc = fitz.open(pdf_path)
    base_name = os.path.basename(pdf_path)
    pdf_name = os.path.splitext(base_name)[0]
    
    if pdfDoc.page_count == 1:
        flag = True
    else:
        flag = False
    
    for pg in range(pdfDoc.page_count):
        page = pdfDoc[pg]
        rotate = int(0)
        zoom_x = 2
        zoom_y = 2
        mat = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
        pix = page.get_pixmap(matrix=mat, alpha=False)

        if not os.path.exists(image_path):
            os.makedirs(image_path)
        
        if flag:
            image_name = pdf_name + '.png'
        else:
            image_name = pdf_name + '_%s.png' % pg
        
        image_path_full = os.path.join(image_path, image_name)
        
        # 使用save方法保存图片
        pix.save(image_path_full)  # 将图片写入指定的文件夹内


if __name__ == "__main__":
    pdfPath = './TJB/餐饮94.pdf'
    imagePath = './img'
    pyMuPDF_fitz(pdfPath, imagePath)
